package com.lineage.server.timecontroller.event;

import com.lineage.server.datatables.lock.VIPReading;
import com.lineage.server.model.L1Object;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.world.World;
import java.sql.Timestamp;
import java.util.Iterator;
import java.util.Map;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class VIPTimer extends TimerTask {
  private static final Log _log = LogFactory.getLog(VIPTimer.class);
  
  private ScheduledFuture<?> _timer;
  
  public void start() {
    int timeMillis = 60000;
    this._timer = GeneralThreadPool.get().scheduleAtFixedRate(this, 60000L, 60000L);
  }
  
  public void run() {
    try {
      Map<Integer, Timestamp> map = VIPReading.get().map();
      if (map.isEmpty())
        return; 
      Timestamp ts = new Timestamp(System.currentTimeMillis());
      Iterator<Integer> iterator = map.keySet().iterator();
      while (iterator.hasNext()) {
        Integer objid = iterator.next();
        Timestamp time = map.get(objid);
        if (time.before(ts)) {
          VIPReading.get().delOther(objid.intValue());
          checkVIP(objid);
        } 
        Thread.sleep(5L);
      } 
    } catch (Exception e) {
      _log.error("VIP计时时间轴异常重启", e);
      GeneralThreadPool.get().cancel(this._timer, false);
      VIPTimer timer = new VIPTimer();
      timer.start();
    } 
  }
  
  private static void checkVIP(Integer objid) {
    try {
      L1Object target = World.get().findObject(objid.intValue());
      if (target != null) {
        boolean isOut = false;
        return;
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
    while (true)
      return; 
  }
}
